﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Routing;
using System.Collections;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Utility;
using System.Drawing;

namespace WebHoiChoMuaBan.Controllers
{
    public class UploadController : Controller
    {
        //
        // GET: /Admin/Upload/
        public ActionResult Index(int? page)
        {
            ViewData["CKEditorFuncNum"] = Request.QueryString["CKEditorFuncNum"];
            ViewData["CKEditor"] = Request.QueryString["CKEditor"];
            ViewData["langCode"] = Request.QueryString["langCode"];

            var dirPath = Path.Combine(Server.MapPath("~/Content/Images/Upload/" + User.Identity.Name), "");
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            if (page == null) page = 1;
            string[] fileNames = Directory.GetFiles(dirPath);
            //Add by To Hoang Duy for sort files by datetime
            IComparer fileComparer = new CompareFiles();
            Array.Sort(fileNames, fileComparer);
            //End of To Hoang Duy

            List<FileInfo> images = new List<FileInfo>();
            //foreach (string filename in fileNames)
            double numPage = Math.Ceiling((double)fileNames.Length/(WebHoiChoMuaBan.Controllers.Global.Config.ROWS_UPLOAD * WebHoiChoMuaBan.Controllers.Global.Config.COLS_UPLOAD));
            ViewData["numPage"] = numPage;
            int numElements = WebHoiChoMuaBan.Controllers.Global.Config.ROWS_UPLOAD*WebHoiChoMuaBan.Controllers.Global.Config.COLS_UPLOAD;
            for (int i = (int)(page-1) * numElements; i < fileNames.Length && i < (int)page*numElements; i++)
            {
                FileInfo image = new FileInfo(fileNames[i]);
                images.Add(image);
            }
            return View(images);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(HttpPostedFileBase file,
            string CKEditorFuncNum, string CKEditor, string langCode)
        {
            if (file.ContentLength > 0)
            {
                var dirPath = Path.Combine(Server.MapPath("~/Content/Images/Upload/" + User.Identity.Name),"");
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(dirPath, fileName);
                file.SaveAs(path);
            }
            return RedirectToAction("/Index", new { CKEditorFuncNum = CKEditorFuncNum , CKEditor = CKEditor, langCode = langCode});
        }

        

        //public void UploadImageToServer(, bool isThumbnailImage)
        //{
        //    ImageHelper imgHelper = new ImageHelper();
        //    if (imgHelper.CheckInfoIcon(hpf))
        //    {
        //        Bitmap image = imgHelper.ResizeImage(hpf.InputStream, width, height);
        //        if (isThumbnailImage)
        //        {
        //            string thumbImgName = "thumbnail_" + hpf.FileName;
        //            var path = Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), thumbImgName);
        //            image.Save(path);
        //            image.Dispose();
        //        }
        //        else
        //        {
        //            var path = Path.Combine(Server.MapPath("~/Content/Images/Upload"), hpf.FileName);
        //            image.Save(path);
        //            image.Dispose();
        //        }
                
        //    }
        //}
    }
}
